package com.yulebaby.callsenter.ui.activity;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yulebaby.callsenter.R;
import com.yulebaby.callsenter.bean.CustomerBean;
import com.yulebaby.callsenter.bean.ResultBean;
import com.yulebaby.callsenter.event.EventFactory;
import com.yulebaby.callsenter.event.MsgEvent;
import com.yulebaby.callsenter.event.RefreshEvent;
import com.yulebaby.callsenter.global.Constant;
import com.yulebaby.callsenter.http.callback.HttpCallback;
import com.yulebaby.callsenter.task.UploadCustomerTask;
import com.yulebaby.callsenter.ui.base.BaseActivity;
import com.yulebaby.callsenter.ui.view.CallDialog;
import com.yulebaby.callsenter.ui.view.PickerDialog;
import com.yulebaby.callsenter.utils.CommonUtil;
import com.yulebaby.callsenter.utils.DateUtil;
import com.yulebaby.callsenter.utils.KeyBoardUtils;
import com.yulebaby.callsenter.utils.PhoneUtil;
import com.yulebaby.callsenter.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.regex.Pattern;


/**
 * Author: Zhangbo
 * Time: 2016年11月25日
 * 考虑不周 未整改  C层与V层糅合在一起了
 */
public class CustomerInfoActivity extends BaseActivity implements View.OnClickListener {

    private TextView mTvEdit;
    private ImageView mIv_back, mIv_callphone;
    private TextView mTv_customername, mTv_addtrack, mTv_looktrack;
    private View mLayout_gender, mLayout_telephone, mLayout_point, mLayout_shopname, mLayout_ascription,
            mLayout_area, mLayout_address, mLayout_level, mLayout_systematics, mLayout_attributive,
            mLayout_communication, mLayout_qq, mLayout_brands, mLayout_source, mLayout_build,
            mLayout_sparephone1, mLayout_sparephone2, mLayout_phone1_mark, mLayout_phone2_mark,
            mLayout_customname, mLayout_allocate, mLayout_weixin, mLayout_scale, mLayout_intention_remark,
            mLayout_individual_remark, mLayout_crucial_remark;
    private CustomerBean customerbean;
    private Toolbar mTbtitle;
    private boolean isEdit;
    private EditText mConectPhoneNum, mConectPhoneNum1, mConectPhoneNum2, mPhone1Mark,
            mPhone2Mark, mCustomname, mWeixinNum, mQqNum, mAddress, mShopName, mIntentionRemark,
            mIndividualRemark, mCrucialRemark;

    public ArrayList<String> phoneNumList;
    private LinearLayout mLl_track;
    private TextView mTvGender;
    private TextView mTvScale;
    private TextView mTvPoint;
    private TextView mTvArea;
    private TextView mTvSystematics;
    private TextView mTvCommunication;
    private TextView mTvBrands;
    private TextView mTvSource;

    private PickerDialog mGenderDialog;
    private PickerDialog mScaleDialog;
    private PickerDialog mPointDialog;
    private PickerDialog mAreaDialog;
    private PickerDialog mSystematicsDialog;
    private PickerDialog mCommunicationDialog;
    private PickerDialog mBrandsDialog;
    private PickerDialog mSourceDialog;

    private ArrayList<View> clickViewList;

    private ArrayList<View> editViewList;


    private ArrayList<View> canSelect;
    private CallDialog mCallDialog;
    private boolean isModify;


    @Override
    protected void onResume() {
        super.onResume();
        KeyBoardUtils.closeKeybord(this, mConectPhoneNum);
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected int initLayoutId() {
        return R.layout.activity_customer_info;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        mTbtitle = (Toolbar) findViewById(R.id.tb_title);
        initToolBar(mTbtitle, false, "");
        // initStatusBar();
        mTvEdit = (TextView) findViewById(R.id.tv_edit);
        mLl_track = (LinearLayout) findViewById(R.id.ll_track);
        mTv_addtrack = (TextView) findViewById(R.id.tv_addtrack);
        mTv_looktrack = (TextView) findViewById(R.id.tv_looktrack);
        if (!isModify) {
            mTvEdit.setVisibility(View.GONE);
            mTv_addtrack.setVisibility(View.GONE);
            mTv_looktrack.setVisibility(View.GONE);
            mLl_track.setVisibility(View.GONE);
        } else {
            mTvEdit.setVisibility(View.VISIBLE);
            mTv_addtrack.setVisibility(View.VISIBLE);
            mTv_looktrack.setVisibility(View.VISIBLE);
            mLl_track.setVisibility(View.VISIBLE);
        }

        mIv_callphone = (ImageView) findViewById(R.id.iv_robcustomer);
        //中间的客户姓名
        mTv_customername = (TextView) findViewById(R.id.tv_customername);


        mIv_back = (ImageView) findViewById(R.id.iv_back);
        //性别
        mLayout_gender = findViewById(R.id.layout_gender);
        //客户姓名
        mLayout_customname = findViewById(R.id.layout_customname);
        //微信
        mLayout_weixin = findViewById(R.id.layout_weixin);
        //规模
        mLayout_scale = findViewById(R.id.layout_scale);
        //意向备注
        mLayout_intention_remark = findViewById(R.id.layout_intention_remark);
        //个性备注
        mLayout_individual_remark = findViewById(R.id.layout_individual_remark);
        //客户关键信息
        mLayout_crucial_remark = findViewById(R.id.layout_crucial_remark);
        //分配时间
        mLayout_allocate = findViewById(R.id.layout_allocate);

        mLayout_telephone = findViewById(R.id.layout_telephone);

        mLayout_sparephone1 = findViewById(R.id.layout_sparephone1);
        mLayout_phone1_mark = findViewById(R.id.layout_phone1_mark);
        mLayout_sparephone2 = findViewById(R.id.layout_sparephone2);
        mLayout_phone2_mark = findViewById(R.id.layout_phone2_mark);

        mLayout_point = findViewById(R.id.layout_point);
        mLayout_shopname = findViewById(R.id.layout_shopname);

        mLayout_ascription = findViewById(R.id.layout_ascription);
        mLayout_area = findViewById(R.id.layout_area);
        mLayout_address = findViewById(R.id.layout_address);

        mLayout_level = findViewById(R.id.layout_level);

        mLayout_systematics = findViewById(R.id.layout_systematics);
        mLayout_attributive = findViewById(R.id.layout_attributive);
        mLayout_communication = findViewById(R.id.layout_communication);
        mLayout_qq = findViewById(R.id.layout_qq);
        mLayout_brands = findViewById(R.id.layout_brands);
        mLayout_source = findViewById(R.id.layout_source);
        mLayout_build = findViewById(R.id.layout_build);
        //可点击View
        clickViewList.add(mLayout_gender);//性别
        clickViewList.add(mLayout_scale);//开店规模
        clickViewList.add(mLayout_point);//签约意向
        clickViewList.add(mLayout_area);//客户地域
        clickViewList.add(mLayout_systematics); //客户分级
        clickViewList.add(mLayout_communication);//沟通方式
        clickViewList.add(mLayout_brands);  //品牌
        clickViewList.add(mLayout_source);//信息来源
        //管理需要显示的View
        canSelect.add(mLayout_gender.findViewById(R.id.iv_canselect));
        canSelect.add(mLayout_scale.findViewById(R.id.iv_canselect));
        canSelect.add(mLayout_point.findViewById(R.id.iv_canselect));
        canSelect.add(mLayout_area.findViewById(R.id.iv_canselect));
        canSelect.add(mLayout_systematics.findViewById(R.id.iv_canselect));
        canSelect.add(mLayout_communication.findViewById(R.id.iv_canselect));
        canSelect.add(mLayout_brands.findViewById(R.id.iv_canselect));
        canSelect.add(mLayout_source.findViewById(R.id.iv_canselect));
        initListener(); //初始化监听
        makeNotClick(); //使需要点击的View初始化的时候不可点击
        showInfo(customerbean);
    }

    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void updateVisitTimes(MsgEvent event) {
        if (event.getIntMsg() != null) {
            customerbean.setVisit_times(event.getIntMsg()); //不为空就更新添加的次数
        }
    }

    /**
     * 使不可点击
     */
    private void makeNotClick() {
        for (View view : clickViewList) {
            view.setClickable(false);
        }
    }

    /**
     * 使可点击
     */
    private void makeClick() {
        for (View view : clickViewList) {
            view.setClickable(true);
        }
    }

    private void initListener() {
        mIv_back.setOnClickListener(this);
        mIv_callphone.setOnClickListener(this);
        mTvEdit.setOnClickListener(this);
        mTv_addtrack.setOnClickListener(this);
        mTv_looktrack.setOnClickListener(this);
        //选项字段 设置点击事件
        for (View view : clickViewList) {
            view.setOnClickListener(this);
        }
    }

    @Override
    protected void initData() {
        phoneNumList = new ArrayList<>();
        clickViewList = new ArrayList<>();
        editViewList = new ArrayList<>();
        canSelect = new ArrayList<>();
        //未优化 使用EventBus RxBus这类比较合适
        Intent intent = getIntent();
        customerbean = (CustomerBean) intent.getSerializableExtra(Constant.CUSTOMER_BEAN);
        isModify = intent.getBooleanExtra(Constant.IS_MODIFY, true);
    }

    /**
     * 初始化对话框
     */
    private void initDialog() {
        mGenderDialog = new PickerDialog(this);
        processContentResult(mTvGender, mGenderDialog);
        mScaleDialog = new PickerDialog(this);
        processContentResult(mTvScale, mScaleDialog);
        mPointDialog = new PickerDialog(this);
        processContentResult(mTvPoint, mPointDialog);
        mAreaDialog = new PickerDialog(this);
        processContentResult(mTvArea, mAreaDialog);
        mSystematicsDialog = new PickerDialog(this);
        processContentResult(mTvSystematics, mSystematicsDialog);
        mCommunicationDialog = new PickerDialog(this);

        processContentResult(mTvCommunication, mCommunicationDialog);
        mBrandsDialog = new PickerDialog(this);

        processContentResult(mTvBrands, mBrandsDialog);
        mSourceDialog = new PickerDialog(this);
        processContentResult(mTvSource, mSourceDialog);
    }

    /**
     * 对接口进行处理
     *
     * @param tv
     * @param genderDialog
     */
    private void processContentResult(final TextView tv, PickerDialog genderDialog) {
        genderDialog.setCurrentContentListener(result -> tv.setText(result));
    }


    public void showInfo(CustomerBean bean) {

        mTv_customername.setText(checkNull(bean.getCustomer_name()));

        ((TextView) mLayout_customname.findViewById(R.id.tv_left)).setText("客户姓名");
        mCustomname = (EditText) mLayout_customname.findViewById(R.id.tv_right);
        mCustomname.setText(checkNull(bean.getCustomer_name()));


        ((TextView) mLayout_telephone.findViewById(R.id.tv_left)).setText("联系电话");
        mConectPhoneNum = (EditText) mLayout_telephone.findViewById(R.id.tv_right);
        mConectPhoneNum.setText(checkNull(bean.getPhone()));

        ((TextView) mLayout_sparephone1.findViewById(R.id.tv_left)).setText("备用电话1");
        mConectPhoneNum1 = (EditText) mLayout_sparephone1.findViewById(R.id.tv_right);
        mConectPhoneNum1.setText(checkNull(bean.getSpare_phone1()));

        ((TextView) mLayout_phone1_mark.findViewById(R.id.tv_left)).setText("备用电话1备注");
        mPhone1Mark = (EditText) mLayout_phone1_mark.findViewById(R.id.tv_right);
        mPhone1Mark.setText(checkNull(bean.getPhone1_ramark()));

        ((TextView) mLayout_sparephone2.findViewById(R.id.tv_left)).setText("备用电话2");
        mConectPhoneNum2 = (EditText) mLayout_sparephone2.findViewById(R.id.tv_right);
        mConectPhoneNum2.setText(checkNull(bean.getSpare_phone2()));

        ((TextView) mLayout_phone2_mark.findViewById(R.id.tv_left)).setText("备用电话2备注");
        mPhone2Mark = (EditText) mLayout_phone2_mark.findViewById(R.id.tv_right);
        mPhone2Mark.setText(checkNull(bean.getPhone2_ramark()));

        ((TextView) mLayout_shopname.findViewById(R.id.tv_left)).setText("店名");
        mShopName = (EditText) mLayout_shopname.findViewById(R.id.tv_right);
        mShopName.setText(checkNull(bean.getShop_name()));

        ((TextView) mLayout_address.findViewById(R.id.tv_left)).setText("地址");
        mAddress = (EditText) mLayout_address.findViewById(R.id.tv_right);
        mAddress.setText(checkNull(bean.getAddress()));

        ((TextView) mLayout_qq.findViewById(R.id.tv_left)).setText("QQ");
        mQqNum = (EditText) mLayout_qq.findViewById(R.id.tv_right);
        mQqNum.setText(checkNull(bean.getQq()));

        ((TextView) mLayout_weixin.findViewById(R.id.tv_left)).setText("微信");
        mWeixinNum = (EditText) mLayout_weixin.findViewById(R.id.tv_right);
        mWeixinNum.setText(checkNull(bean.getWeixin()));


        //意向备注
        ((TextView) mLayout_intention_remark.findViewById(R.id.tv_top)).setText("意向备注");
        mIntentionRemark = (EditText) mLayout_intention_remark.findViewById(R.id.et_bottom);
        mIntentionRemark.setText(checkNull(bean.getIntention_remark()));

        //个性备注
        ((TextView) mLayout_individual_remark.findViewById(R.id.tv_top)).setText("个性备注");
        mIndividualRemark = (EditText) mLayout_individual_remark.findViewById(R.id.et_bottom);
        mIndividualRemark.setText(checkNull(bean.getIndividual_remark()));

        //客户关键信息
        ((TextView) mLayout_crucial_remark.findViewById(R.id.tv_top)).setText("客户关键信息");
        mCrucialRemark = (EditText) mLayout_crucial_remark.findViewById(R.id.et_bottom);
        mCrucialRemark.setText(checkNull(bean.getInformation()));


        //选项字段
        ((TextView) mLayout_gender.findViewById(R.id.tv_left)).setText("性别");
        mTvGender = (TextView) mLayout_gender.findViewById(R.id.tv_right);
        mTvGender.setText(checkNull(bean.getSex()));


        ((TextView) mLayout_scale.findViewById(R.id.tv_left)).setText("开店规模");
        mTvScale = (TextView) mLayout_scale.findViewById(R.id.tv_right);
        mTvScale.setText(checkNull(bean.getScale()));


        ((TextView) mLayout_point.findViewById(R.id.tv_left)).setText("签约意向");


        String intention = bean.getIntention();
        mTvPoint = (TextView) mLayout_point.findViewById(R.id.tv_right);
        setIntentionStyle(intention);
        mTvPoint.setText(intention);

        ((TextView) mLayout_area.findViewById(R.id.tv_left)).setText("客户地域");
        mTvArea = (TextView) mLayout_area.findViewById(R.id.tv_right);
        mTvArea.setText(checkNull(bean.getZone()));


        ((TextView) mLayout_systematics.findViewById(R.id.tv_left)).setText("客户分级");
        mTvSystematics = (TextView) mLayout_systematics.findViewById(R.id.tv_right);
        mTvSystematics.setText(checkNull(bean.getLevel()));

        ((TextView) mLayout_communication.findViewById(R.id.tv_left)).setText("沟通方式");
        mTvCommunication = (TextView) mLayout_communication.findViewById(R.id.tv_right);
        mTvCommunication.setText(checkNull(bean.getContact_method()));

        ((TextView) mLayout_brands.findViewById(R.id.tv_left)).setText("品牌");
        mTvBrands = (TextView) mLayout_brands.findViewById(R.id.tv_right);
        mTvBrands.setText(checkNull(bean.getBrand()));

        ((TextView) mLayout_source.findViewById(R.id.tv_left)).setText("信息来源");
        mTvSource = (TextView) mLayout_source.findViewById(R.id.tv_right);
        mTvSource.setText(checkNull(bean.getFrom()));

        //不变动字段
        ((TextView) mLayout_attributive.findViewById(R.id.tv_left)).setText("归属员工");
        ((TextView) mLayout_attributive.findViewById(R.id.tv_right)).setText(checkNull(bean.getBelong_sale()));

        ((TextView) mLayout_ascription.findViewById(R.id.tv_left)).setText("归属区域");
        ((TextView) mLayout_ascription.findViewById(R.id.tv_right)).setText(checkNull(bean.getGroupName()));

        ((TextView) mLayout_build.findViewById(R.id.tv_left)).setText("建档时间");
        ((TextView) mLayout_build.findViewById(R.id.tv_right)).setText(checkNull(DateUtil.formatDateTimeDetail(bean.getCreate_time())));

        ((TextView) mLayout_allocate.findViewById(R.id.tv_left)).setText("分配时间");
        ((TextView) mLayout_allocate.findViewById(R.id.tv_right)).setText(checkNull(DateUtil.formatDateTimeDetail(bean.getAlloc_time())));

        ((TextView) mLayout_level.findViewById(R.id.tv_left)).setText("客户阶段");
        ((TextView) mLayout_level.findViewById(R.id.tv_right)).setText(checkNull(bean.getStep()));

        //可编辑View
        editViewList.add(mCustomname);//客户姓名
        editViewList.add(mConectPhoneNum);//联系电话
        editViewList.add(mConectPhoneNum1);//备用电话1 + 备注
        editViewList.add(mPhone1Mark);
        editViewList.add(mConectPhoneNum2);//备用电话2 + 备注
        editViewList.add(mPhone2Mark);
        editViewList.add(mCrucialRemark);//客户关键信息
        editViewList.add(mIndividualRemark);//个性备注
        editViewList.add(mIntentionRemark);//意向备注
        editViewList.add(mShopName);//店名
        editViewList.add(mAddress);//地址
        editViewList.add(mWeixinNum);//微信
        editViewList.add(mQqNum);//QQ
        //初始化对话框
        initDialog();
    }

    /**
     * 根据签约意向 显示对应的颜色和图片
     *
     * @param intention
     */
    private void setIntentionStyle(String intention) {
        switch (intention) {
            case "强烈":
                mTvPoint.setTextColor(getResources().getColor(R.color.strongpoint));
                mIv_callphone.setImageResource(R.drawable.callstrong_btn_selector);
                customerbean.setStressIntent(Constant.StressIntent.STATE_STRONG);
                break;
            case "准强烈":
                mTvPoint.setTextColor(getResources().getColor(R.color.middlepoint));
                mIv_callphone.setImageResource(R.drawable.callmiddle_btn_selector);
                customerbean.setStressIntent(Constant.StressIntent.STATE_MIDDLE);
                break;
            case "中等":
                mTvPoint.setTextColor(getResources().getColor(R.color.normal));
                mIv_callphone.setImageResource(R.drawable.callnormal_btn_selector);
                customerbean.setStressIntent(Constant.StressIntent.STATE_COMMONLY);
                break;
            case "较弱":
                mTvPoint.setTextColor(getResources().getColor(R.color.normal));
                mIv_callphone.setImageResource(R.drawable.callnormal_btn_selector);
                customerbean.setStressIntent(Constant.StressIntent.STATE_LOW);
                break;
            case "无":
                mTvPoint.setTextColor(getResources().getColor(R.color.normal));
                mIv_callphone.setImageResource(R.drawable.callnormal_btn_selector);
                customerbean.setStressIntent(Constant.StressIntent.STATE_NONE);
                break;
            case "":
                mTvPoint.setTextColor(getResources().getColor(R.color.normal));
                mIv_callphone.setImageResource(R.drawable.callnormal_btn_selector);
                break;


        }
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_robcustomer:
                if (customerbean != null) {
                    //选择前清空一下集合
                    phoneNumList.clear();
                    checkHasPhoneNum(customerbean);
                    switchCallNum();
                }
                break;
            case R.id.tv_edit:
                if (isEdit) {
                    saveEditChange();
                } else {
                    startEdit();
                }

                break;
            case R.id.layout_gender://性别
                showPickDialog(mGenderDialog, CommonUtil.getStringArray(R.array.picker_gender));
                break;
            case R.id.layout_scale://开店规模
                showPickDialog(mScaleDialog, CommonUtil.getStringArray(R.array.picker_scale));
                break;
            case R.id.layout_point://签约意向
                showPickDialog(mPointDialog, CommonUtil.getStringArray(R.array.picker_intention));
                break;
            case R.id.layout_area://客户地域
                showPickDialog(mAreaDialog, CommonUtil.getStringArray(R.array.picker_address));
                break;
            case R.id.layout_systematics: //客户分级
                showPickDialog(mSystematicsDialog, CommonUtil.getStringArray(R.array.picker_level));
                break;
            case R.id.layout_communication: //沟通方式
                showPickDialog(mCommunicationDialog, CommonUtil.getStringArray(R.array.picker_communication));
                break;
            case R.id.layout_brands: //品牌
                showPickDialog(mBrandsDialog, CommonUtil.getStringArray(R.array.picker_brand));
                break;
            case R.id.layout_source://信息来源
                showPickDialog(mSourceDialog, CommonUtil.getStringArray(R.array.picker_from));
                break;
            case R.id.tv_addtrack://添加售前跟踪
                Intent addintent = new Intent(this, AddPreSaleRemarkActivity.class);
                addintent.putExtra(Constant.CUSTOMER_BEAN, customerbean);
                startActivity(addintent);
                break;
            case R.id.tv_looktrack://查看跟踪记录
                Intent preintent = new Intent(this, PreSaleRemarkAcitivity.class);
                preintent.putExtra(Constant.CUSTOMER_BEAN, customerbean);
                startActivity(preintent);
                break;
        }
    }

    /**
     * 显示Dialog
     *
     * @param values
     */
    private void showPickDialog(PickerDialog dialog, String[] values) {
        if (KeyBoardUtils.getKeybordState(this)) {
            KeyBoardUtils.closeKeybord(this);
        }
        if (dialog == null) {
            dialog = new PickerDialog(this);
        }
        if (dialog.isShowing()) {
            dialog.dismiss();
        } else {
            dialog.setCancelable(true);
            dialog.setCanceledOnTouchOutside(true);
            dialog.show();
            dialog.initPicker(values);
        }
    }

    /**
     * 显示可选择的箭头
     */
    private void showSelectArrow() {
        for (View view : canSelect) {
            view.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 显示可选择的箭头
     */
    private void hideSelectArrow() {
        for (View view : canSelect) {
            view.setVisibility(View.GONE);
        }
    }

    /**
     * 开启编辑模式
     */
    private void startEdit() {

        mTvEdit.setText("保存");
        mLl_track.setVisibility(View.GONE);
        makeClick();
        isEdit = !isEdit;

        editTextRequestFocus(mCustomname);
//        KeyBoardUtils.openKeybord(this, mCustomname);
        //设置可编辑
        for (View view : editViewList) {
            view.setEnabled(true);
        }
        showSelectArrow();
        setEditMode();
        //可点击可滚动,下方的选项列表消失 TODO
    }

    /**
     * 设置编辑模式
     */
    private void setEditMode() {
        mConectPhoneNum.setInputType(InputType.TYPE_CLASS_PHONE);
        mConectPhoneNum1.setInputType(InputType.TYPE_CLASS_PHONE);
        mConectPhoneNum2.setInputType(InputType.TYPE_CLASS_PHONE);
        mQqNum.setInputType(InputType.TYPE_CLASS_NUMBER);
        mConectPhoneNum.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
        mConectPhoneNum1.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
        mConectPhoneNum2.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
        mPhone1Mark.setFilters(new InputFilter[]{new InputFilter.LengthFilter(12)});
        mPhone2Mark.setFilters(new InputFilter[]{new InputFilter.LengthFilter(12)});
        mCustomname.setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});

        setMultiEditText(mCrucialRemark);
        setMultiEditText(mIndividualRemark);
        setMultiEditText(mIntentionRemark);
    }

    /**
     * EditText的自动换行 设置最大行数为3行
     */
    private void setMultiEditText(EditText et) {
        et.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
        et.setSingleLine(false);
        et.setMaxLines(3);
        et.setHorizontallyScrolling(false);
        et.setGravity(Gravity.TOP);
    }

    /**
     * Edittext获取焦点
     *
     * @param editText
     */
    private void editTextRequestFocus(EditText editText) {
        //将光标放在Edittext的最后   TODO 失效
        editText.setSelection(editText.getText().length());
        editText.getSelectionStart();
        editText.setFocusable(true);
        editText.setFocusableInTouchMode(true);
        editText.requestFocus();
    }

    /**
     * 对编辑后的变化进行保存
     */
    private void saveEditChange() {
        String qq = mQqNum.getText().toString().trim();
        String phoneNum = mConectPhoneNum.getText().toString().trim();
        String phoneNum1 = mConectPhoneNum1.getText().toString().trim();
        String phoneNum2 = mConectPhoneNum2.getText().toString().trim();
        if (!TextUtils.isEmpty(qq)) {
            if (qq.length() < 5 || qq.length() >= 13) {
                ToastUtil.showToast("QQ号码不正确");
                return;
            }
        }
        if (!TextUtils.isEmpty(phoneNum)) {
            if (!isNumeric(phoneNum)) {
                ToastUtil.showToast("手机号码不正确");
                return;
            }
        }
        if (!TextUtils.isEmpty(phoneNum1)) {
            if (!isNumeric(phoneNum1)) {
                ToastUtil.showToast("手机号码不正确");
                return;
            }
        }
        if (!TextUtils.isEmpty(phoneNum2)) {
            if (!isNumeric(phoneNum2)) {
                ToastUtil.showToast("手机号码不正确");
                return;
            }
        }

        //调用更新用户信息的接口
        uploadInfo();
    }

    public boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    /**
     * 更新用户信息
     */
    private void uploadInfo() {
        String name = mCustomname.getText().toString().trim();

        String phoneNum = mConectPhoneNum.getText().toString().trim();
        String phoneNum1 = mConectPhoneNum1.getText().toString().trim();
        String phoneNum2 = mConectPhoneNum2.getText().toString().trim();
        String phoneMark1 = mPhone1Mark.getText().toString().trim();
        String phoneMark2 = mPhone2Mark.getText().toString().trim();
        String shop = mShopName.getText().toString().trim();
        String adress = mAddress.getText().toString().trim();
        String weixin = mWeixinNum.getText().toString().trim();
        String qq = mQqNum.getText().toString().trim();
        String details = mCrucialRemark.getText().toString().trim();
        String intentionRemark = mIntentionRemark.getText().toString().trim();
        String individualRemark = mIndividualRemark.getText().toString().trim();
        //选择
        String gender = mTvGender.getText().toString().trim();
        String scale = mTvScale.getText().toString().trim();
        String point = mTvPoint.getText().toString().trim();
        String area = mTvArea.getText().toString().trim();
        String systematics = mTvSystematics.getText().toString().trim();
        String communication = mTvCommunication.getText().toString().trim();
        String brands = mTvBrands.getText().toString().trim();
        String source = mTvSource.getText().toString().trim();
        setIntentionStyle(point);//根据文字设置颜色

        //顶上显示的名字
        mTv_customername.setText(name);
        customerbean.setCustomer_name(name);
        customerbean.setShop_name(shop);
        customerbean.setAddress(adress);
        customerbean.setWeixin(weixin);
        customerbean.setQq(qq);
        customerbean.setPhone(phoneNum);
        customerbean.setSex(gender);
        customerbean.setScale(scale);
        customerbean.setIntention(point);
        customerbean.setZone(area);
        customerbean.setLevel(systematics);
        customerbean.setContact_method(communication);
        customerbean.setBrand(brands);
        customerbean.setFrom(source);
        customerbean.setSpare_phone1(phoneNum1);
        customerbean.setPhone1_ramark(phoneMark1);
        customerbean.setSpare_phone2(phoneNum2);
        customerbean.setPhone2_ramark(phoneMark2);
        customerbean.setInformation(details);
        customerbean.setIntention_remark(intentionRemark);
        customerbean.setIndividual_remark(individualRemark);

        //上传给服务器
        UploadCustomerTask.doUploadCustomerInfo(this, customerbean, new HttpCallback<ResultBean>() {
            @Override
            public void onSuccess(ResultBean result) {
                ToastUtil.showToast("更新用户资料成功");
                updateViewState();
                EventBus.getDefault().post(EventFactory.CreateRefreshEvent(RefreshEvent.refreshDataFromInternet));
            }
        });

    }

    /**
     * 保存成功后界面进行更新
     */
    private void updateViewState() {
        //设置不可点击
        makeNotClick();
        isEdit = !isEdit;
        mTvEdit.setText("编辑");
        mLl_track.setVisibility(View.VISIBLE);
        //设置不可编辑
        for (View view : editViewList) {
            view.setEnabled(false);
        }
        hideSelectArrow();
        KeyBoardUtils.closeKeybord(CustomerInfoActivity.this, mConectPhoneNum);
    }

    /**
     * 选择想要拨打的电话号码
     */
    private void switchCallNum() {
        if (phoneNumList.size() == 1) {
            PhoneUtil.callPhones(phoneNumList.get(0));
        } else {
            mCallDialog = new CallDialog(this, phoneNumList, customerbean);
            mCallDialog.setNumListener(num -> PhoneUtil.callPhones(num));
            mCallDialog.show();
        }
    }

    /**
     * 检查是否包含备用号码
     *
     * @param customerBean
     */
    private void checkHasPhoneNum(CustomerBean customerBean) {
        String phone = customerBean.getPhone();
        String spare_phone1 = customerBean.getSpare_phone1();
        String spare_phone2 = customerBean.getSpare_phone2();
        phoneNumList.add(phone);

        if (!TextUtils.isEmpty(spare_phone1)) {
            phoneNumList.add(spare_phone1);
        }

        if (!TextUtils.isEmpty(spare_phone2)) {
            phoneNumList.add(spare_phone2);
        }
    }

    private void initStatusBar() {
        mTbtitle.setFitsSystemWindows(true);
        mTbtitle.setClipToPadding(false);
    }

    /**
     * 对显示结果进行非空判断
     *
     * @param str
     * @return
     */
    private String checkNull(String str) {
        if (TextUtils.isEmpty(str)) {
            str = "";
        }
        return str;
    }

}
